/* GLMorph.m created by epeyton on Thu 03-Dec-1998 */

#import "GLMorph.h"

extern int mono;
extern int smooth;
extern int object;

@implementation GLMorph	

- (id)initWithFrame:(NSRect)frameRect
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    self = [super initWithFrame:frameRect];
    if (self) {
        _view = [[NSOpenGLView alloc] initWithFrame:frameRect];
        [self addSubview:_view];
    }
    
    [NSBundle loadNibNamed:@"GLMorph.nib" owner:self];
    mono = [defaults integerForKey:@"GLMorphMono"];
    smooth = [defaults integerForKey:@"GLMorphUnshaded"];
    object = [defaults integerForKey:@"GLMorphObjectType"];

    if (!object) object = 1;
    
    [isMono setState:mono];
    [isUnshaded setState:smooth];
    [objectArray selectCellWithTag:object];


    return self;
}

- (NSTimeInterval)animationTimeInterval
{
    return 0.01;
}

- (void)setFrameSize:(NSSize)newSize
{
    [super setFrameSize:newSize];
    [_view setFrameSize:newSize];
    _initedGL = NO;

}

- (void)drawRect:(NSRect)rects
{
    
    [_view lockFocus];
    
    if (!_initedGL) {
        pinit();
        reshape((int)rects.size.width, (int)rects.size.height);

        _initedGL = YES;
    }

    [_view unlockFocus];
}


- (void)oneStep
{
    if ([_view canDraw]) {
        [_view lockFocus];
        draw();
        [_view unlockFocus];
    }

    
    return;
}


- (BOOL)hasConfigureSheet { return YES; }
- (NSWindow*)configureSheet { return window; }

- (void)closeSheet:(id)sender
{
    [NSApp endSheet:window];
}

- (void)updateSelection:sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    object = [objectArray selectedRow] + 1;
    mono = [isMono state];
    smooth = [isUnshaded state];

    [defaults setInteger:object forKey:@"GLMorphObjectType"];
    [defaults setInteger:mono forKey:@"GLMorphMono"];
    [defaults setInteger:smooth forKey:@"GLMorphUnshaded"];
    
    pinit();

}

@end